﻿#include "newformulawidget.h"
#include "ui_newformulawidget.h"
#include "readonlydelegate.h"
#include <QMessageBox>

NewFormulaWidget::NewFormulaWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewFormulaWidget)
{
    ui->setupUi(this);
    setWindowTitle("新建公式");
    model = new NewFormulaStandardItemModel();
    model->setWidgetType(NewFormulaStandardItemModel::NewFormmula);
    combineProduct = nullptr;

    ui->tableView->setModel(model);

    for(int i=0;i<model->columnCount();i++){
        if(i!=5)
            ui->tableView->setItemDelegateForColumn(i, new ReadOnlyDelegate());
    }

    setSelectionRow();

    QPalette bluePalette1;
    bluePalette1.setColor(QPalette::Text, Qt::blue);
    QPalette bluePalette2;
    bluePalette2.setColor(QPalette::WindowText, Qt::blue);
    ui->labelCount->setPalette(bluePalette2);
    ui->lineEditCount->setPalette(bluePalette1);

    connect(&productPicker, &ProductPicker::singleSelected,
            [=](Product *product, int defaultCount)
    {
        if(model->currentProducts().size() != 0){
            for(Product *selectedProduct:model->currentProducts()){
                if(selectedProduct->productId == product->productId){
                    QMessageBox::warning(this, "警告", "选择的商品已作为拆分商品！");
                    return;
                }
            }
        }
        if(defaultCount<1) defaultCount=1;

        combineProduct = product;
        ui->lineEditType->setText(product->type);
        ui->lineEditName->setText(product->name);
        ui->lineEditUnit->setText(product->unit);
        combineProduct->count = defaultCount;
        ui->lineEditCount->setText(QString("%1").arg(defaultCount));
    });

    connect(&productPicker, &ProductPicker::selectedFinished,
            [=](QList<Product*> products, int defaultCount)
    {
        if(combineProduct != nullptr){
            int index=-1;
            for(int i=0;i<products.size();i++){
                if(combineProduct->productId == products[i]->productId){
                    index = i;
                    break;
                }
            }
            if(index != -1){
                QMessageBox::warning(this, "警告", "拆分商品中存在总成商品，已自动剔除");
                products.removeAt(index);
            }
        }
        model->insertData(products, defaultCount);
    });

    connect(ui->tableView, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        if(index.row() == model->rowCount()-1){
            productPicker.setSelectionMode(QAbstractItemView::MultiSelection);
            productPicker.showAndInit();
        }
    });

    connect(ui->lineEditType, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        productPicker.setSelectionMode(QAbstractItemView::SingleSelection);
        productPicker.showAndInit();
    });

    connect(ui->lineEditName, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        productPicker.setSelectionMode(QAbstractItemView::SingleSelection);
        productPicker.showAndInit();
    });

    connect(ui->lineEditUnit, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        productPicker.setSelectionMode(QAbstractItemView::SingleSelection);
        productPicker.showAndInit();
    });
}

NewFormulaWidget::~NewFormulaWidget()
{
    delete ui;
}

void NewFormulaWidget::showAndInit(){
    clear();
    exec();
}

void NewFormulaWidget::clear(){
    model->reset();
    setSelectionRow();
    ui->lineEditType->clear();
    ui->lineEditName->clear();
    ui->lineEditUnit->clear();
    ui->lineEditCount->clear();

    combineProduct = nullptr;
}

void NewFormulaWidget::setSelectionRow()
{
    model->appendRow(*(new QList<QStandardItem*>));
    ui->tableView->setSpan(model->rowCount()-1, 0, 1, model->columnCount());
}

void NewFormulaWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void NewFormulaWidget::on_pushButtonReset_clicked()
{
    clear();
}

void NewFormulaWidget::on_pushButtonSubmit_clicked()
{
    productService.addFormula(this, combineProduct, ui->lineEditCount->text().toInt(), model->currentProducts());
}
